<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%><%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@page import="com.spmanage.util.WebAction"%>
<%@page import="com.spmanage.util.StringUtil"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
	WebAction webAction = new WebAction(request,response);
    System.out.println("空中接口-报告同步");
    String userPhone = webAction.getParameterString("userPhone");
    String channel = webAction.getParameterString("channel");
    String linkId = webAction.getParameterString("linkId");
    String fee = webAction.getParameterString("fee");
    String status = webAction.getParameterString("status");
    String msgId = webAction.getParameterString("msgId");
    String srvCode = webAction.getParameterString("srvCode");
    String feeType = webAction.getParameterString("feeType");
    String date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());//webAction.getParameterString("date");
    String org = SqlUtil.getStringResult("select org from "+BillingUtil.getSendDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    if(org!=null&&org.length()>0) {
    	System.out.println("该linkid已经有同步状态");
    	return;
    }
    //如果是0资费，但是成功的，就算成失败的
    if(fee.equals("0")){
    	status="UNDELIVRD";
    }
    
    String spNo = SqlUtil.getStringResult("select dest from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
    String content = SqlUtil.getStringResult("select body from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);

	//指令处理,返回处理后的指令
	String strMo = BillingUtil.getMo(spNo, content);
    //获取用户渠道号
    int channelId = BillingUtil.getUserChannel(spNo,strMo);
    int spid = BillingUtil.getUserSpId(spNo,strMo);
    //获取是否要扣量
	int ischeck = BillingUtil.getCheckSend(linkId);
    int provinceId = BillingUtil.getProvinceId(userPhone);
    
    StringBuilder sb = new StringBuilder();
    sb.append("'");
    sb.append(spid);
    sb.append("',");
    sb.append("1");//msgmode
    sb.append(",");
    sb.append("15");//fmt
    sb.append(",");
    sb.append("0");//futype
    sb.append(",");
    sb.append("2");//ftype
    sb.append(",");
    sb.append(fee);//feecode  
    sb.append(",");
    sb.append("'");
    sb.append("XXLYYY");//stype
    sb.append("',");
    sb.append("1");//num
    sb.append(",");
    sb.append("'");
    sb.append(userPhone);
    sb.append("',");
    sb.append("'");
    sb.append(spNo);
    sb.append("',");
    sb.append("'");
    sb.append(userPhone);
    sb.append("',");
    sb.append("getDate()");  //tm
    sb.append(",");
    sb.append("'");
    sb.append("mid");  //mid
    sb.append("',");
    sb.append("'");
    sb.append("msgid");  //msgid
    sb.append("',");
    sb.append("0");//result  DELIVRD
    sb.append(",");
    sb.append("'");
    sb.append(status);//DELIVRD
    sb.append("',");
    sb.append("'");
    sb.append(channelId);//channelId
    sb.append("',");
    sb.append("'");
    sb.append(linkId);//linkid
    sb.append("','"+content+"'");
    sb.append(",");
    sb.append(provinceId);
    sb.append(",");
    sb.append(ischeck);
        
    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
    System.out.println("空中端口:"+spNo+";指令："+content+";spid:"+spid+",channel:"+channelId+"");
    System.out.println("空中接口-报告同步是否需要扣量："+ischeck+",linkid:"+linkId);
    if(ischeck == 0) {//不扣量
    	System.out.println("空中-状态同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5)+"；渠道号："+channelId);
    	String mtUrl = BillingUtil.getMtUrlByChannel(channelId);
		//如果cp地址中含有'?'+参数的，就用'&'
    	if(mtUrl.indexOf("?")>0){
			mtUrl = mtUrl+"&";
		}else{    			
    		mtUrl = mtUrl+"?";
		}
    	mtUrl = mtUrl+"spNo="+spNo+"&userPhone="+userPhone+"&linkId="+linkId+"&fee="+fee+"&status="+status+"&date="+date+"&mo="+URLEncoder.encode(content,"UTF-8")+"";
    	if(status.equals("DELIVRD")&&!fee.equals("0")){
    		String str=HttpUtil.getHtmlSource(mtUrl,"UTF-8");
    		System.out.println("空中-同步出去的地址:"+str);
    		
    	}
    }
    
    
    response.getWriter().write("OK");
%>
